<?php
declare(strict_types=1);


namespace App\Fastapp\Mapper;

use App\Fastapp\Model\Message;
use Hyperf\Database\Model\Builder;
use Mine\Abstracts\AbstractMapper;

/**
 * 系统消息列表Mapper类
 */
class FastappMessageMapper extends AbstractMapper
{
    /**
     * @var Message
     */
    public $model;

    public function assignModel()
    {
        $this->model = Message::class;
    }

    /**
     * 搜索处理器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch(Builder $query, array $params): Builder
    {
        // 入口类型
        if (isset($params['account_type']) && filled($params['account_type'])) {
            $query->where('account_type', '=', $params['account_type']);
        }

        // 消息类型
        if (isset($params['type']) && filled($params['type'])) {
            $query->where('type', '=', $params['type']);
        }

        // 数据类型1链接2视频3分享4文章
        if (isset($params['object_type']) && filled($params['object_type'])) {
            $query->where('object_type', '=', $params['object_type']);
        }

        return $query->orderByDesc('id');
    }
}